package com.raising.modules.code.controller;

import com.google.common.collect.Maps;
import com.raising.ConstantParam;
import com.raising.StaticParam;
import com.raising.modules.code.entity.CodeTemplate;
import com.raising.modules.code.service.CodeTemplateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.bind.annotation.*;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.utils.DictUtils;

import com.raising.utils.JStringUtils;

import com.raising.modules.code.entity.CodeCatalog;
import com.raising.modules.code.service.CodeCatalogService;

import java.util.List;
import java.util.Map;

/**
 * 目录 控制器
 * @author gaoyuan
 * @createTime 2018-01-03 14:15:39
 */
@Controller
@RequestMapping("${adminPath}/code/codeCatalog")
public class CodeCatalogController extends BaseController {

    @Autowired
    private CodeCatalogService codeCatalogService;
    @Autowired
    private CodeTemplateService codeTemplateService;
    /**
     * 获取单个对象（共用）
     * @author gaoyuan
     * @createTime 2018-01-03 14:15:39
     */
    @ModelAttribute
    public CodeCatalog get(@RequestParam(required = false) String id) {
        CodeCatalog entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = codeCatalogService.get(id);
        }
        if (entity == null) {
            entity = new CodeCatalog();
        }
        return entity;
    }
    /**
    * 跳转至列表页面
    * @author gaoyuan
    * @createTime 2018-01-03 14:15:39
    */
    @RequiresPermissions("code:codeCatalog:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "code/codeCatalog/list";
    }

    /**
    * 加载列表分页数据
    * @author gaoyuan
    * @createTime 2018-01-03 14:15:39
    */
    @RequiresPermissions("code:codeCatalog:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(Model model, CodeCatalog codeCatalog,Page<CodeCatalog> page) {
        page.setEntity(codeCatalog);
        page.setOrderBy(" a.id asc ");
        return new ResultVo(ResultVo.SUCCESS,"ok",codeCatalogService.getPage(page));
    }

    /**
     * 加载 目录类别与模板列表
     * @author gaoyuan
     * @createTime 2018-01-03 14:15:39
     */
    @RequiresPermissions("code:codeCatalog:view")
    @RequestMapping(value = "/getCatalogAndTemplateList.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getCatalogAndTemplateList(CodeCatalog codeCatalog) {
        List<CodeCatalog> codeCatalogList = codeCatalogService.getList(codeCatalog);
        CodeTemplate codeTemplate = new CodeTemplate();
        codeTemplate.setProjectId(codeCatalog.getProjectId());
        List<CodeTemplate> codeTemplateList = codeTemplateService.getList(codeTemplate);
        Map<String,Object> map = Maps.newHashMap();
        map.put("codeCatalogList",codeCatalogList);
        map.put("codeTemplateList",codeTemplateList);

        return new ResultVo(ResultVo.SUCCESS,"ok",map);
    }

    /**
    * 跳转新增页面
    * @author gaoyuan
    * @createTime 2018-01-03 14:15:39
    */
    @RequiresPermissions("code:codeCatalog:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(CodeCatalog codeCatalog,Model model) {
        model.addAttribute("codeCatalog", codeCatalog);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "code/codeCatalog/add";
    }

    /**
    * 执行新增操作
    * @author gaoyuan
    * @createTime 2018-01-03 14:15:39
    */
    @RequiresPermissions("code:codeCatalog:create")
    @RequestMapping(value = "/create.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo create(String parentId,String name,String projectId, RedirectAttributes redirectAttributes) {
        CodeCatalog parentCodeCatalog = codeCatalogService.get(parentId);
        CodeCatalog codeCatalog = new CodeCatalog();
        if(name!=null){
            name = JStringUtils.trim(name);
        }
        codeCatalog.setName(name);
        codeCatalog.setAvailable(StaticParam.YES_STRING);
        codeCatalog.setStatus(StaticParam.VALID_STRING);
        //准备插入的就是顶级的
        if(parentCodeCatalog == null){
            codeCatalog.setParentId("0");
            codeCatalog.setProjectId(projectId);
            codeCatalog.setParentIds("0/");
        }else{
            codeCatalog.setParentId(parentId);
            codeCatalog.setProjectId(parentCodeCatalog.getProjectId());
            codeCatalog.setParentIds(parentCodeCatalog.getParentIds() + parentId + ConstantParam.SEPARATOR);
        }


        codeCatalogService.save(codeCatalog);
        addMessage(redirectAttributes,"新增成功");
        return new ResultVo(ResultVo.SUCCESS,ResultVo.CODE_OK,"成功","");
    }
    /**
    * 跳转更新页面
    * @author gaoyuan
    * @createTime 2018-01-03 14:15:39
    */
    @RequiresPermissions("code:codeCatalog:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(CodeCatalog codeCatalog, Model model) {
        model.addAttribute("codeCatalog", codeCatalog);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "code/codeCatalog/edit";
    }
    /**
    * 执行更新操作
    * @author gaoyuan
    * @createTime 2018-01-03 14:15:39
    */
    @RequiresPermissions("code:codeCatalog:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(CodeCatalog codeCatalog, RedirectAttributes redirectAttributes) {
        codeCatalogService.save(codeCatalog);
        addMessage(redirectAttributes,"修改成功");
        return "redirect:"+ adminPath +"/code/codeCatalog/update?id="+codeCatalog.getId();
    }
    /**
    * 执行删除操作
    * @author gaoyuan
    * @createTime 2018-01-03 14:15:39
    */
    @RequiresPermissions("code:codeCatalog:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        codeCatalogService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/code/codeCatalog/list?pageNo="+pageNo+"&pageSize="+pageSize;
    }

    /**
     * 执行删除操作
     * @author gaoyuan
     * @createTime 2018-01-03 14:15:39
     */
    @RequiresPermissions("code:codeCatalog:delete")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo delete(String id) {
        //删除之前先检查一遍是否有下级目录或模板
        CodeCatalog codeCatalog = new CodeCatalog();
        codeCatalog.setParentId(id);
        List<CodeCatalog> list = codeCatalogService.getList(codeCatalog);
        if(list.size()>0){
            return new ResultVo(ResultVo.FAILURE,ResultVo.CODE_NO_PARAMS,"请先删除下级目录或模板","");
        }
        CodeTemplate codeTemplate = new CodeTemplate();
        codeTemplate.setCatalog(id);
        List<CodeTemplate> templateList = codeTemplateService.getList(codeTemplate);
        if(templateList.size()>0){
            return new ResultVo(ResultVo.FAILURE,ResultVo.CODE_NO_PARAMS,"请先删除下级目录或模板","");
        }
        codeCatalogService.delete(id);
        return new ResultVo(ResultVo.SUCCESS,ResultVo.CODE_OK,"成功","");
    }
}
